package com.exam.web.action.subject;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.exam.web.action.BaseAction;
import com.exam.web.action.privilege.Privilege;
import com.exam.bean.HardEasy;
import com.exam.bean.PageView;
import com.exam.bean.QueryResult;
import com.exam.bean.QuestionType;
import com.exam.bean.subject.KnowledgePoint;
import com.exam.bean.subject.OptionItem;
import com.exam.bean.subject.Question;
import com.exam.service.subject.KnowledgePointService;
import com.exam.service.subject.QuestionService;
import com.opensymphony.xwork2.ActionContext;

@Controller("questionAction")
@Scope("prototype")
public class QuestionAction extends BaseAction {
	
	private static final long serialVersionUID = -5605445941610677727L;

	@Resource
	private QuestionService questionService;
	@Resource
	private KnowledgePointService knowledgePointService;
	
	private PageView<Question> pageView;//记录集
	private Question question;//题目
	private Integer id;//id
	private Integer pointid;//知识点id
	private String type;//题型
	private String hardEasy;//难度
	private Integer[] questionids;
	private List<OptionItem> items;
	
	private List<KnowledgePoint> menupoints;
	private List<KnowledgePoint> points;
	
	
	/**
	 * 题目显示
	 * @return
	 * @throws Exception
	 */
	@Privilege(model="question",privilegeValue="view")
	public String list()throws Exception{
		
		PageView<Question> pageView=new PageView<Question>(12,this.getPage());
		int firstIndex=(this.getPage()-1)*pageView.getMaxResult();
		
		LinkedHashMap<String, String> orderby=new LinkedHashMap<String, String>();
		orderby.put("id", "desc");
		
		if("true".equals(this.getQuery())){
			StringBuffer jpql=new StringBuffer("");
			List<Object> params=new ArrayList<Object>();
			if(this.getType()!=null&&!"".equals(this.getType().trim())){
				jpql.append("o.type = ?"+ (params.size()+1));
				params.add(QuestionType.valueOf(this.getType()));
			}System.out.println(this.getType());
			if(this.getQuestion().getTitle()!=null && !"".equals(this.getQuestion().getTitle().trim())){
				if(this.getType()!=null&&!"".equals(this.getType().trim())){
					jpql.append(" and ");
				}
				jpql.append("o.title like ?"+ (params.size()+1));
				params.add("%"+ this.getQuestion().getTitle().trim()+ "%");
				jpql.append(" or o.addtitle like ?"+ (params.size()+1));
				params.add("%"+ this.getQuestion().getTitle().trim()+ "%");
				jpql.append(" or o.point.name like ?"+ (params.size()+1));
				params.add("%"+ this.getQuestion().getTitle().trim()+ "%");
				
				QueryResult<Question> qr=questionService.getScrollData(Question.class
						,firstIndex,pageView.getMaxResult(),jpql.toString(),params.toArray(),orderby);
				
				pageView.setQueryResult(qr);
				this.setPageView(pageView);
				return SUCCESS;
			}
		}
		
		QueryResult<Question> qr=questionService.getScrollData(Question.class
				,firstIndex,pageView.getMaxResult(),orderby);
		pageView.setQueryResult(qr);
		this.setPageView(pageView);
		return SUCCESS;
	}

	
	/**
	 * 添加题目
	 * @return
	 */
	@Privilege(model="question",privilegeValue="insert")
	public String add()throws Exception{
		
		Question question = this.getQuestion();
		if(this.getPointid()!=null&&this.getPointid()>0){
			question.setPoint(new KnowledgePoint(this.getPointid()));
		}
		question.setType(QuestionType.valueOf(this.getType()));
		question.setHardEasy(HardEasy.valueOf(this.getHardEasy()));
		OptionItem[] a=new OptionItem[4];int i=0;
		for(OptionItem item : this.getItems()){
			a[i]=new OptionItem(item.getItem(),item.getOptions());
			question.addItem(a[i++]);//http://taoistwar.javaeye.com/blog/388916
			//System.out.println(item.getItem()+":"+item.getOptions());
		}System.out.println(a[0].equals(a[1]));
		//questionService.save(question);
		
		ActionContext.getContext().put("message", "添加题目成功");
		ActionContext.getContext().put("urladdress", "question_list.html");
		return "message";
	}
	
	
	/**
	 * 添加题目界面
	 * @return
	 */
	@Privilege(model="question",privilegeValue="insert")
	public String addUI()throws Exception{
		
		return this.getType();
	}
	
	
	/**
	 * 修改题目
	 * @return
	 */
	@Privilege(model="question",privilegeValue="update")
	public String edit()throws Exception{
		
		Question question=questionService.find(Question.class, this.getId());
		
		
		questionService.update(question);
		ActionContext.getContext().put("message", "修改题目成功");
		ActionContext.getContext().put("urladdress", "question_list.html");
		return "message";
	}
	
	
	/**
	 * 修改题目界面
	 * @return
	 */
	@Privilege(model="question",privilegeValue="update")
	public String editUI()throws Exception{
		
		Question question=questionService.find(Question.class, this.getId());
		this.setQuestion(question);
		this.setType(question.getType().toString());
		this.setHardEasy(question.getHardEasy().toString());
		List<OptionItem> temps=new ArrayList<OptionItem>();
		for(OptionItem item : question.getItems()){
			temps.add(item);
		}
		this.setItems(temps);
		return "edit";
	}
	
	
	/**
	 * 查看题目界面
	 * @return
	 */
	@Privilege(model="question",privilegeValue="view")
	public String viewUI()throws Exception{
		
		Question question=questionService.find(Question.class, this.getId());
		this.setQuestion(question);
		return "view";
	}
	
	/**
	 * 选择知识点界面
	 * @return
	 * @throws Exception
	 */
	public String selectUI()throws Exception{
		
		String jpql = "o.parent is null and o.visible=true";
		Object[] params = new Object[0];
		if(this.getPointid()!=null && this.getPointid()>0){
			jpql = "o.parent.id=?1 and o.visible=true";
			params = new Object[]{this.getPointid()};
			KnowledgePoint point = knowledgePointService.find(KnowledgePoint.class, this.getPointid());
			KnowledgePoint parent = point.getParent();
			List<KnowledgePoint> menupoints = new ArrayList<KnowledgePoint>();
			menupoints.add(point);
			while(parent!=null){
				menupoints.add(parent);
			    parent =  parent.getParent();
			}
			this.setMenupoints(menupoints);
		}
		this.setPoints(knowledgePointService.getScrollData(KnowledgePoint.class, jpql, params).getResultList());
		return "select";
	}
	
	
	
	/**
	 * 启用题目
	 * @return
	 * @throws Exception
	 */
	@Privilege(model="question",privilegeValue="visible")
	public String visible()throws Exception{
		
		questionService.setVisible(this.getQuestionids(), true);
		ActionContext.getContext().put("message", "启用成功");
		ActionContext.getContext().put("urladdress", "employee_list.html");
		return "message";
	}
	
	/**
	 * 禁用题目
	 * @return
	 * @throws Exception
	 */
	@Privilege(model="employee",privilegeValue="disvisible")
	public String disvisible()throws Exception{
		
		questionService.setVisible(this.getQuestionids(), false);
		ActionContext.getContext().put("message", "禁用成功");
		ActionContext.getContext().put("urladdress", "employee_list.html");
		return "message";
	}


	public PageView<Question> getPageView() {
		return pageView;
	}
	public void setPageView(PageView<Question> pageView) {
		this.pageView = pageView;
	}

	public Question getQuestion() {
		return question;
	}
	public void setQuestion(Question question) {
		this.question = question;
	}

	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}

	public Integer getPointid() {
		return pointid;
	}
	public void setPointid(Integer pointid) {
		this.pointid = pointid;
	}

	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}

	public String getHardEasy() {
		return hardEasy;
	}
	public void setHardEasy(String hardEasy) {
		this.hardEasy = hardEasy;
	}

	public Integer[] getQuestionids() {
		return questionids;
	}
	public void setQuestionids(Integer[] questionids) {
		this.questionids = questionids;
	}

	public List<OptionItem> getItems() {
		return items;
	}
	public void setItems(List<OptionItem> items) {
		this.items = items;
	}

	public List<KnowledgePoint> getMenupoints() {
		return menupoints;
	}
	public void setMenupoints(List<KnowledgePoint> menupoints) {
		this.menupoints = menupoints;
	}

	public List<KnowledgePoint> getPoints() {
		return points;
	}
	public void setPoints(List<KnowledgePoint> points) {
		this.points = points;
	}
	
	
}
